<?php
    //!!!!!need rhino installed 
    define("SOLOR_RHINO_PATH", "/usr/bin/rhino");
    //require_once('dig.all.php');
    /**
    * 用于提取 56 网站的视频
    *
    */
    class W56Analyzer extends BasicAnalyzer implements Analyzer
    {
        public $features = array("url" => "/www.56.com\/u/i");

        public function analyze($content, $url)
        {
            // url 
            $matches = null;
            preg_match('/id="input_page_url".*value="http:\/\/www.56.com\/.*?\/(.*?)_\/(\d+)_\/(\d+)_\/(.*?)_\/(.*?)_(.*?)_\/.*.swf/i', $content, $matches);

            /*
array(7) {
  [0]=>
  string(126) "id="input_page_url" type="text" value="http://www.56.com/n_v48_/c38_/5_/1_/seveen_/zhajm_121025522843_/248000_/0_/34488231.swf"
  [1]=>
  string(3) "c38"
  [2]=>
  string(1) "5"
  [3]=>
  string(1) "1"
  [4]=>
  string(6) "seveen"
  [5]=>
  string(5) "zhajm"
  [6]=>
  string(12) "121025522843"
}

            */
            if (!empty($matches))
            {

                $cache_var = $matches[1];
                $farm_var = $matches[2];
                $host_var = $matches[3];
                $user_var = $matches[4];
                $title_var = $matches[5];
                $id_var = $matches[6];

                $vurl = "http://f1.r.56.com/f1.{$cache_var}.56.com/flvdownload/{$farm_var}/{$host_var}/{$user_var}@56.com_56flv_{$title_var}_{$id_var}.flv";

                preg_match('/<title>(.*?)-(.*?)<\/title>/i', $content, $matches);

                $author = iconv("gbk//ignore", "utf-8//ignore", $matches[1]);
                $title = iconv("gbk//ignore", "utf-8//ignore", $matches[2]);

                $category = "";

                $date_str = iconv("utf-8", "gbk", "上传日期：");
                preg_match("/{$date_str}(.*?)<br>/i", $content, $matches);
                $pub_date = $matches[1];

                $descr =$title;

                $tags = array('sr_56');

                $referer = $url;

                $ext= "flv";
                
                $rv = array(
                    array(
                        "url" => $vurl, 
                        "title" => $title,
                        "descr" => $descr,
                        "referer" => $referer,
                        "tags" => $tags, 
                        "category" => $category,
                        "ext" => $ext,
                        "pub_date" => $pub_date,
                        "author" => $author
                        )
                    );
            }
            else
            {
                
                preg_match("/var _oFlv_o.*?;/i", $content, $matches);
                if (empty($matches))
                {
                    return array();
                }
                $js_var = $matches[0];
                
                $jscode = <<<JS
                $js_var
function decode(str, second)
{
    var c1;
    var c2;
    var c3;
    var c4;
    var c5=[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
    var c6 = str.charCodeAt(0);
    var c7,i, len, out;
    if(!second){
        second = 1;
        c7=decode(str, second);
        str = c7.substr(c6);
    };
    len = str.length;
    i = 1;
    out = "";
    while(i < len)
    { do
      {
         c1 = c5[str.charCodeAt(i++) & 0xff];
      } while(i < len && c1 == -1);
      if(c1 == -1)
          break;
      do {
          c2 = c5[str.charCodeAt(i++) & 0xff];
      } while(i < len && c2 == -1);
      if(c2 == -1) break;
      out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
      do {
          c3 = str.charCodeAt(i++) & 0xff;
          if(c3 == 61) return out;
          c3 = c5[c3];
      } while(i < len && c3 == -1);
      if(c3 == -1) break;
      out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
      do {
          c4 = str.charCodeAt(i++) & 0xff;
          if(c4 == 61)
              return out;
          c4 = c5[c4];
      } while(i < len && c4 == -1);
      if(c4 == -1)
          break;
      out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
};
print (decode(_oFlv_o, 0));
JS;
                $temp = tempnam("/tmp", "solor56");
                file_put_contents($temp, $jscode);
                $rhino_command = SOLOR_RHINO_PATH . " $temp";
                $json = `$rhino_command`;
                unlink($temp);
                $json_data = json_decode($json, 1);
                $id = $json_data['id'];
                $promo_url = ("http://www.56.com/u93/share.php?id=$id&p=1");
                $new_page = $this->getPage($promo_url);

                preg_match("/id=\"code2\".*?value=\"http:\/\/www.56.com\/.*?\/(.*?)_\/(\d+)_\/(\d+)_\/(.*?)_\/(.*?)_(.*?)_\/.*.swf\"/i", $new_page, $matches);
                if (empty($matches))
                {
                    return array();
                }

                $cache_var = $matches[1];
                $farm_var = $matches[2];
                $host_var = $matches[3];
                $user_var = $matches[4];
                $title_var = $matches[5];
                $id_var = $matches[6];

                $vurl = "http://f1.r.56.com/f1.{$cache_var}.56.com/flvdownload/{$farm_var}/{$host_var}/{$user_var}@56.com_56flv_{$title_var}_{$id_var}.flv";
                preg_match('/document.title = "(.*?)[-,](.*?)"/i', $content, $matches);
                $title = @ iconv("gbk//ignore", "utf-8//ignore", $matches[1]);
                if (empty($title))
                {
                    preg_match('/<title>(.*?)<\/title>/i', $content, $matches);
                    $title = iconv("gbk//ignore", "utf-8//ignore", $matches[1]);
                }
                @ preg_match('/<li id="videoinfo_l"><span>.*?<\/span>(.*?)<\/li>/i', $content, $matches);
                @ $descr = iconv("gbk//ignore", "utf-8//ignore", $matches[1]);
                $referer = $url;
                $tags = array("sr_56");
                $category = "";
                $ext = "flv";
                preg_match('/<span class="hui">(.*?)<\/span><\/dd>/i', $content, $matches);
                $pub_date = $matches[1];
                $author = "未知";

                $rv = array(array(
                        "url" => $vurl, 
                        "title" => $title,
                        "descr" => $descr,
                        "referer" => $referer,
                        "tags" => $tags, 
                        "category" => $category,
                        "ext" => $ext,
                        "pub_date" => $pub_date,
                        "author" => $author
                    ));//preg_match
            }

            
            //===================================

            return $rv;
        }
        function test($url)
        {
            $content = $this->getPage($url);
            return $this->analyze($content, $url);
        }
    }
    // $a = new W56Analyzer();
    // var_dump($a->test("http://www.56.com/u95/v_MzQ1MTMyNDQ.html"));
?>
